/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011      Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
 
http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.javascript;

import net.youmi.android.AdManager;
import net.youmi.android.spot.SpotDialogListener;
import net.youmi.android.spot.SpotManager;

import org.cocos2dx.Tower.R;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

import android.app.AlertDialog;
import android.content.pm.ActivityInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;


// The name of .so is specified in AndroidMenifest.xml. NativityActivity will load it automatically for you.
// You can use "System.loadLibrary()" to load other .so files.

public class AppActivity extends Cocos2dxActivity{

    static String hostIPAdress = "0.0.0.0";
	private static AppActivity app = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        if(nativeIsLandScape()) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        }
        if(nativeIsDebug()){
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        hostIPAdress = getHostIpAddress();
		
		app = this;
		
		//youmi ad initial.
		AdManager.getInstance(this).init("c1eb451895d7f098", "ac52a03ecd1f4a52", false);
		SpotManager.getInstance(this).loadSpotAds();
		SpotManager.getInstance(this).setSpotOrientation(
	            SpotManager.ORIENTATION_PORTRAIT);
		SpotManager.getInstance(this).setAnimationType(SpotManager.ANIM_SIMPLE);
		SpotManager.getInstance(this).showSpotAds(this, new SpotDialogListener() {
		    @Override
		    public void onShowSuccess() {
		        Log.d("Youmi", "onShowSuccess");
		    }

		    @Override
		    public void onShowFailed() {
		        Log.d("Youmi", "onShowFailed");
		    }

		    @Override
		    public void onSpotClosed() {
		        Log.d("sdkDemo", "closed");
		    }

			@Override
			public void onSpotClick() {
				// TODO Auto-generated method stub
				Log.d("sdkDemo", "clickedß");
			}
		});
    }
    
    public static void showAd(){
    	 app.runOnUiThread(new Runnable() {
             @Override
             public void run() {
            	 Toast.makeText(app, "注意！点击广告即会开始下载广告展示的应用！点击右上角关闭广告！", Toast.LENGTH_LONG).show();
            	 SpotManager.getInstance(app).showSpotAds(app);
             }
    	 });
    	
    }
	public static void showShare(final int floorNumber) {

        //杩欓噷涓�瀹氳浣跨敤runOnUiThread
        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
            	ShareSDK.initSDK(app);
            	 OnekeyShare oks = new OnekeyShare();
            	 //关闭sso授权
            	 oks.disableSSOWhenAuthorize(); 
            	 
            	// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
            	 //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
            	 // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
            	 oks.setTitle("快来玩" + app.getString(R.string.app_name) + "！");
            	 // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
            	 oks.setTitleUrl("http://www.1-xing.com/1dieta.html");
            	 // text是分享文本，所有平台都需要这个字段
            	 oks.setText("我在游戏中叠了" + floorNumber + "层！住持都夸我棒，你也快来试试吧！");
            	 oks.setImageUrl("http://dashboard.mob.com/Uploads/7f284b43e94c5f3bb0257319ef1dd7ca.png");
            	 // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
            	 //oks.setImagePath("/data/data/org.cocos2dx.Tower/files/src/icon.png");//确保SDcard下面存在此张图片
            	 // url仅在微信（包括好友和朋友圈）中使用
            	 oks.setUrl("http://www.1-xing.com/1dieta.html");
            	 // comment是我对这条分享的评论，仅在人人网和QQ空间使用
            	 oks.setComment("太好玩了！快来试试！");
            	 // site是分享此内容的网站名称，仅在QQ空间使用
            	 oks.setSite("益行");
            	 // siteUrl是分享此内容的网站地址，仅在QQ空间使用
            	 oks.setSiteUrl("http://www.1-xing.com/1dieta.html");
            	 
            	// 启动分享GUI
            	 oks.show(app);
            }
        });
    }
	
    @Override
    public Cocos2dxGLSurfaceView onCreateView() {
        Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
        // TestCpp should create stencil buffer
        glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);

        return glSurfaceView;
    }

    public String getHostIpAddress() {
        WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        int ip = wifiInfo.getIpAddress();
        return ((ip & 0xFF) + "." + ((ip >>>= 8) & 0xFF) + "." + ((ip >>>= 8) & 0xFF) + "." + ((ip >>>= 8) & 0xFF));
    }
    
    public static String getLocalIpAddress() {
        return hostIPAdress;
    }
    
    private static native boolean nativeIsLandScape();
    private static native boolean nativeIsDebug();
    
}
